package com.sea.finance.controller;


import com.alibaba.excel.util.StringUtils;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.finance.entity.VtFile;
import com.sea.finance.service.VtFileService;
import com.sea.finance.service.VtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 运费
 * @author 陈鸿飞
 */
@Controller
@RequestMapping(value = "/sea/finance/vtFile")
public class VtFileController {
    @Autowired
    private VtFileService vtFileService;
    @Autowired
    private VtService vtService;


    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     VtFile vtFile) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<VtFile> list = vtFileService.findByCnd(vtFile);
        PageInfo<VtFile> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    @RequestMapping(value ="/dealImportSku", method = RequestMethod.POST)
    @ResponseBody
    public AjaxReturn dealImportSku() {
        AjaxReturn result = Ajax.fail().setMsg("生成失败！");
        try {
            //开始处理导入数据
            vtFileService.dealImportSku();


            result.setOk(true).setMsg("生成成功！<br />");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }



    @RequestMapping(value ="/dealFileStatus", method = RequestMethod.POST)
    @ResponseBody
    public AjaxReturn dealFileStatus(@RequestParam("dealIds") Integer[] ids) {
        AjaxReturn result = Ajax.fail().setMsg("生成失败！");
        try{
            if (null != ids && ids.length>0) {
                vtFileService.updateByIds(ids);
                result.setOk(true).setMsg("数据处理中，请稍后查询结果！<br />");

            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxReturn delete(Integer id) {
        AjaxReturn result = Ajax.fail().setMsg("删除失败！");
        try{
            if (null != id && vtFileService.delete(id) > 0) {
                result.setOk(true).setMsg("删除成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

}
